package com.example.example5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Intent it = null;
    private Button start;
    private Button stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = findViewById(R.id.btnStart);
        start.setOnClickListener(this);
        stop = findViewById(R.id.btnStop);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // 显示意图
//        it = new Intent(MainActivity.this, MyAndroidService.class);
        // 隐示意图
        it = new Intent();
        it.setAction("com.example.example5.mp3");
        it.setPackage("com.example.example5");
        switch (view.getId()){
            case R.id.btnStart:
                startService(it);
                Toast.makeText(this, "start music", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnStop:
                stopService(it);
                Toast.makeText(this, "stop music", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "onDestroy: ");
        it = new Intent(this, MyAndroidService.class);
        stopService(it);
        finish();
    }

}